#!/bin/sh

#build环境 dev/release
environment=${1:-}
domain=http://p3v2omddo.bkt.clouddn.com/
qiniuToolPath=./qiniu

#计时
SECONDS=0

buildIos(){
	ionic cordova build ios
	cordova-hcp build
}

uploadQiniu(){

	if [ "$environment" = "dev" ]; then
		echo "dev状态------>不上传更新到七牛"
  	return
	fi

	rm -rf  $qiniuToolPath/ionic-website/www
	cp -rf ./www $qiniuToolPath/ionic-website
	
	# upload
	$qiniuToolPath/qshell-darwin-x64 qupload -success-list $qiniuToolPath/success.txt $qiniuToolPath/lizhi_upload_to_ionic.conf 
}

cdnQiniu(){

	
	if [ "$environment" = "dev" ]; then
		echo "dev状态------>不刷新CDN"
  	return
	fi
	
	#list all
	#$qiniuToolPath/qshell-darwin-x64 listbucket ionic-web $qiniuToolPath/ionic-web-file.txt
	
	#del all
	#$qiniuToolPath/qshell-darwin-x64 batchdelete -force ionic-web $qiniuToolPath/ionic-web-file.txt
	
	rm -rf  $qiniuToolPath/torefresh-file.txt || true
	
	cat $qiniuToolPath/success.txt | while read line
	do
	    #echo "File:${line}"
	    echo $domain${line##*	}
	    echo $domain${line##*	} >> $qiniuToolPath/torefresh-file.txt
	done


	# cdn refresh
	$qiniuToolPath/qshell-darwin-x64 cdnrefresh $qiniuToolPath/torefresh-file.txt
	
	#$qiniuToolPath/qshell-darwin-x64 cdnrefresh -dirs $qiniuToolPath/torefresh-dirs.txt 
	

}


#执行命令
buildIos
uploadQiniu
cdnQiniu

echo "*** 打包完成 *** Total time: ${SECONDS}s"


